package com.lovo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.bean.Buyorder;
import com.lovo.bean.Detail;
import com.lovo.bean.Tenderinfo2;
import com.lovo.dto.SupplierMsgDto;
import com.lovo.dto.TenderInfoDto;
import com.lovo.mapper.BuyorderMapper;
import com.lovo.service.FSupplierService;
import com.lovo.service.IBuyorderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.service.IDetailService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;

/**
 * <p>
 * ??Ӧ?? 服务实现类
 * </p>
 *
 * @author lovo
 * @since 2022-09-17
 */
@Service
@Transactional
public class BuyorderServiceImpl extends ServiceImpl<BuyorderMapper, Buyorder> implements IBuyorderService {


    @Autowired
    IDetailService detailService;

    @Autowired
    FSupplierService fSupplierService;

    @Override
    public IPage<Buyorder> findAllBuyOrder(Integer pageNO) {

        return this.getBaseMapper().selectPage(new Page(pageNO, 5), null);
    }

    @Override
    public void updateBuyOrderStatusById(Integer id) {
        QueryWrapper<Buyorder> qw = new QueryWrapper<>();
        qw.eq("buyOrder", id);
        Buyorder buyorder = this.getBaseMapper().selectOne(qw);
        buyorder.setBStatus("已入库");
        this.getBaseMapper().updateById(buyorder);
//远程调用供应商修改供应商收货单状态
        fSupplierService.hdUpdate(id);
       //多供应商有问题待修改

        Detail detail = new Detail();
        detail.setGoodsid(buyorder.getProductid());
        detail.setDSize(buyorder.getBSize());
        detail.setDBatch(buyorder.getBBatch());
        detail.setGoodsname(buyorder.getProductname());
        detail.setNum(buyorder.getBNum());
        detail.setDCode(buyorder.getBCode());
        detail.setDPrice(buyorder.getBPrice());
        detail.setDType(buyorder.getBType());
        detailService.sendDetail(detail);
    }


    @RabbitListener(queues = "supplierMsgQueue")
    public void sendMsg(Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag, SupplierMsgDto supplierMsgDto) throws IOException {

        Buyorder buyorder = new Buyorder();
        buyorder.setBuyorder(supplierMsgDto.getBuyid());
        buyorder.setBCode(supplierMsgDto.getMCode());
        buyorder.setBName(supplierMsgDto.getSuppliername());
        buyorder.setBType(supplierMsgDto.getMType());
        buyorder.setBStatus("未入库");
        buyorder.setBAddress(supplierMsgDto.getMAddress());
        buyorder.setProductname(supplierMsgDto.getProductname());
        buyorder.setBNum(supplierMsgDto.getMNum());
        buyorder.setBBatch(supplierMsgDto.getMBatch());
        buyorder.setBSize(supplierMsgDto.getMSize());
        buyorder.setProductid(supplierMsgDto.getProductid());
        buyorder.setSupplierid(supplierMsgDto.getSupplierid());
        buyorder.setBPrice(supplierMsgDto.getMPrice());
        buyorder.setBTime(supplierMsgDto.getMTime());
        getBaseMapper().insert(buyorder);
        //手动确认
        channel.basicAck(tag, false);

    }


}
